package main.com.oaer.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import main.com.oaer.common.HibernateDaoUtil;
import main.com.oaer.dao.IndexDao;
import main.com.oaer.model.Purview;

@Repository
public class IndexDaoImpl implements IndexDao {

	@Autowired
	private  HibernateDaoUtil hibernateDaoUtil;
	
	@Override
	public List<Purview> findFirstPurviewByUserId(Long id) {
		if(id==null) return null;
		String hql=" select " +
				" purview from " +
				" Purview purview, " +
				" RolePurview rolePurview, " +
				" UserRole userRole " +
				" where userRole.userId=? " +
				" and userRole.roleId=rolePurview.roleId " +
				" and rolePurview.purviewId=purview.id " +
				" and purview.level=1";
		return hibernateDaoUtil.findListByHQL(hql, id);
	}

	@Override
	public List<Purview> findUserPurviewByParentId(Long id, Long parentId) {
		if(id==null || parentId==null) return null;
		String hql=" select " +
		" purview from " +
		" Purview purview, " +
		" RolePurview rolePurview, " +
		" UserRole userRole " +
		" where " +
		" userRole.userId=? " +
		" and " +
		" userRole.roleId=rolePurview.roleId " +
		" and " +
		" rolePurview.purviewId=purview.id " +
		" and " +
		" purview.parentId=?";
		return hibernateDaoUtil.findListByHQL(hql,id, parentId);
	}

}
